package model

import (
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(30)" json:"username"`
	Password string `gorm:"type:char(52)" json:"password"`
	Phone    string `gorm:"type:char(11)" json:"phone"`
}

func (u *User) GetUserByPhone(db *gorm.DB, phone string) error {
	return db.Where("phone=?", phone).Limit(1).Find(&u).Error
}
func (u *User) AddUser(db *gorm.DB) error {
	return db.Create(&u).Error
}

type Friend struct {
	UserId   int `gorm:"type:int" json:"userId"`
	FriendId int `gorm:"type:int" json:"friendId"`
	Status   int `gorm:"not null;decimal 0;commit:状态,0:未通过，1:已通过"`
}

func (f *Friend) IsFriend(db *gorm.DB, userId, FriendId int) bool {
	var count int64
	db.Where("user_id=? and friend_id=?", userId, FriendId).Count(&count)
	return count > 0
}
func (f *Friend) AddFriend(db *gorm.DB) error {
	return db.Create(&f).Error
}
